package com.android.clinic.app.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by cheng on 2018/1/26.
 */
public class GroupChooseView {

    private LinearLayout layParent;
    private Context context;
    private int childCount = 5;
    public final static int TYPE_TEXT = 3;
    public final static int TYPE_IMAGE = 1;
    private int showType;
    private int selectedImgResource = android.R.drawable.radiobutton_on_background;
    private int unselectedImgResource = android.R.drawable.radiobutton_off_background;
    private int textOnColor = Color.WHITE;
    private int textOffColor = Color.BLACK;
    private int textSize = 15;
    private LinearLayout.LayoutParams params;
    private int oldIndex = -1;
    private int margin = 8;

    /**
     * GroupChooseView
     */
    public GroupChooseView(Context context, LinearLayout layParent, int type) {
        super();
        this.context = context;
        this.layParent = layParent;
        this.showType = type;
    }

    public void setCount(int count) {
        this.childCount = count;
    }

    public void setImageResource(int onImgResource, int offImgResource) {
        this.selectedImgResource = onImgResource;
        this.unselectedImgResource = offImgResource;
    }

    public void setTextValue(int onColor, int offColor, int size) {
        this.textOnColor = onColor;
        this.textOffColor = offColor;
        this.textSize = size;
    }

    public void setMargin(int margin) {
        this.margin = margin;
    }

    public void initView() {
        if (params == null) {
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(margin, margin, margin, margin);
        }
        layParent.removeAllViews();
        if (childCount < 1) {
            return;
        }
        if (showType == TYPE_TEXT) {
            createTextView();
        } else {
            createView();
        }
        selectedViewIndex(0);
    }

    public void setParams(LinearLayout.LayoutParams params) {
        this.params = params;
    }

    private void createView() {
        ImageView view;
        for (int i = 0; i < childCount; i++) {
            view = new ImageView(context);
            view.setImageResource(unselectedImgResource);
            layParent.addView(view, params);
        }
    }

    private void createTextView() {
        TextView textView;
        for (int i = 0; i < childCount; i++) {
            textView = new TextView(context);
            textView.setGravity(Gravity.CENTER);
            textView.setText(String.valueOf(i + 1));
            textView.setTextColor(this.textOffColor);
            textView.setTextSize(this.textSize);
            textView.setBackgroundResource(unselectedImgResource);
            layParent.addView(textView, params);
        }
    }

    public void selectedViewIndex(int index) {
        if (index == oldIndex) {
            return;
        }
        if (oldIndex != -1) {
            View oldSelectedView = layParent.getChildAt(oldIndex);
            if (oldSelectedView instanceof TextView) {
                ((TextView) oldSelectedView).setTextColor(this.textOffColor);
                oldSelectedView.setBackgroundResource(unselectedImgResource);
            } else if (oldSelectedView instanceof ImageView) {
                ((ImageView) oldSelectedView)
                        .setImageResource(unselectedImgResource);
            }
        }
        oldIndex = index;
        View selectedView = layParent.getChildAt(index);
        if (selectedView instanceof TextView) {
            ((TextView) selectedView).setTextColor(this.textOnColor);
            selectedView.setBackgroundResource(selectedImgResource);
        } else if (selectedView instanceof ImageView) {
            ((ImageView) selectedView).setImageResource(selectedImgResource);
        }
    }

}
